package com.example.textswitchtestdemo01.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.Material;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.example.textswitchtestdemo01.util.RetrofitUtil;
import com.google.gson.Gson;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SeventhActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = "SeventhActivity";
    private ImageView img_shop;
    private LinearLayout tv_price_seven;
    private LinearLayout tv_count_seven;
    private ListView lv_seventh;
    private List<Material.DataBean> list = new ArrayList<>();
    private ArrayAdapter<Material.DataBean> arrayAdapter;
    private ImageView img_price_seven;
    private ImageView img_count_seven;
    private boolean priceSort = true;
    private boolean countSort = true;
    private SwipeRefreshLayout srl_seventh;
    private List<Material.DataBean> shoppingList = new ArrayList<>();
    private Intent intent;
    private Button btn_buy;
    private final DecimalFormat DF = new DecimalFormat("#,###");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seventh);
        initView();
        InitToolbar.initToolbar(this, "第七页", true);
        intent = new Intent(this, SeventhShopActivity.class);

        getData();
    }

    private void getData() {
        RetrofitUtil.getAllMaterial(new Callback<Material>() {
            @Override
            public void onResponse(Call<Material> call, Response<Material> response) {
                Toast.makeText(SeventhActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();

                List<Material.DataBean> data = response.body().getData();
                if (data != null) {
                    list.clear();
                    list.addAll(data);
                }
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<Material> call, Throwable t) {
                Toast.makeText(SeventhActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        img_shop = (ImageView) findViewById(R.id.img_shop);
        img_shop.setOnClickListener(this);
        tv_price_seven = findViewById(R.id.ll_price_seven);
        tv_price_seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (priceSort) {
                    img_price_seven.setImageResource(R.drawable.btn_up);
                    if (!list.isEmpty()) {
                        Collections.sort(list, new Comparator<Material.DataBean>() {
                            @Override
                            public int compare(Material.DataBean o1, Material.DataBean o2) {
                                return o1.getPrice() - o2.getPrice();
                            }
                        });
                    }
                } else {
                    img_price_seven.setImageResource(R.drawable.btn_down);
                    if (!list.isEmpty()) {
                        Collections.sort(list, new Comparator<Material.DataBean>() {
                            @Override
                            public int compare(Material.DataBean o1, Material.DataBean o2) {
                                return o2.getPrice() - o1.getPrice();
                            }
                        });
                    }
                }
                priceSort = !priceSort;
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(SeventhActivity.this, "sort done", Toast.LENGTH_SHORT).show();
            }
        });
        tv_count_seven = findViewById(R.id.ll_count_seven);
        tv_count_seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (countSort) {
                    img_count_seven.setImageResource(R.drawable.btn_up);
                    if (!list.isEmpty()) {
                        Collections.sort(list, new Comparator<Material.DataBean>() {
                            @Override
                            public int compare(Material.DataBean o1, Material.DataBean o2) {
                                return o1.getNum() - o2.getNum();
                            }
                        });
                    }
                } else {
                    img_count_seven.setImageResource(R.drawable.btn_down);
                    if (!list.isEmpty()) {
                        Collections.sort(list, new Comparator<Material.DataBean>() {
                            @Override
                            public int compare(Material.DataBean o1, Material.DataBean o2) {
                                return o2.getNum() - o1.getNum();
                            }
                        });
                    }
                }
                arrayAdapter.notifyDataSetChanged();
                countSort = !countSort;
                Toast.makeText(SeventhActivity.this, "sort done", Toast.LENGTH_SHORT).show();
            }
        });

        img_price_seven = (ImageView) findViewById(R.id.img_price_seven);
        img_count_seven = (ImageView) findViewById(R.id.img_count_seven);


        lv_seventh = (ListView) findViewById(R.id.lv_seventh);
        arrayAdapter = new ArrayAdapter<Material.DataBean>(getApplicationContext(), R.layout.item_lv_seven, list) {


            class ViewHolder {
                TextView tv_count_lv_seven;
                TextView tv_price_lv_seven;
                TextView tv_gongyingshang_lv_seven;
                TextView tv_name_lv_seven;
                TextView tv_id_lv_seven;
            }

            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_lv_seven, parent, false);
                    holder = new ViewHolder();
                    holder.tv_count_lv_seven = convertView.findViewById(R.id.tv_count_lv_seven);
                    holder.tv_gongyingshang_lv_seven = convertView.findViewById(R.id.tv_gongyingshang_lv_seven);
                    holder.tv_id_lv_seven = convertView.findViewById(R.id.tv_id_lv_seven);
                    holder.tv_name_lv_seven = convertView.findViewById(R.id.tv_name_lv_seven);
                    holder.tv_price_lv_seven = convertView.findViewById(R.id.tv_price_lv_seven);

                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                Material.DataBean item = getItem(position);
                holder.tv_price_lv_seven.setText(DF.format(item.getPrice()));
                holder.tv_name_lv_seven.setText(item.getMaterialName());
                holder.tv_gongyingshang_lv_seven.setText(item.getContent());
                holder.tv_id_lv_seven.setText(item.getId() + "");
                holder.tv_count_lv_seven.setText(item.getNum() + "");
                return convertView;
            }
        };
        lv_seventh.setAdapter(arrayAdapter);


        srl_seventh = (SwipeRefreshLayout) findViewById(R.id.srl_seventh);
        srl_seventh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
                srl_seventh.setRefreshing(false);
            }
        });

        btn_buy = (Button) findViewById(R.id.btn_buy);
        btn_buy.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_shop:
                startActivity(intent);
                break;
            case R.id.btn_buy:
                buySome();
                break;
        }
    }

    private void buySome() {
        if (!list.isEmpty()) {
//            Collections.sort(list, new Comparator<Material.DataBean>() {
//                @Override
//                public int compare(Material.DataBean o1, Material.DataBean o2) {
//                    return o1.getPrice() - o2.getPrice();
//                }
//            });
            shoppingList.clear();
            for (int i = 0; i < 3; i++) {
                shoppingList.add(list.get(i));
            }

            Material material = new Material();
            material.setData(shoppingList);
//            material.setMessage("ok");
//            material.setStatus(200);
            String json = new Gson().toJson(material);
            Log.d(TAG, "buySome: " + json);
            intent.putExtra("json", json);

            Toast.makeText(this, "buySome", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "list is Empty", Toast.LENGTH_SHORT).show();
        }


    }
}
